//
//  shareViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/23.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "shareViewController.h"
#import "SGQRCodeTool.h"
#import "Masonry.h"

@interface shareViewController ()
{

    NSString *invitation;
    
}
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *imageHeadView;

@end

@implementation shareViewController
- (void)viewWillAppear:(BOOL)animated{

    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"推广二维码"];
    [self creationDate];
    self.imageHeadView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64)];
    
    [self.view addSubview:self.imageHeadView];
    self.imageHeadView.image = [UIImage imageNamed:@"qrcodebg"];
    [self creationView];
    
    // 生成二维码(中间带有图标)
    
    
    
}
- (void)creationView{
    self.view.backgroundColor = [UIColor colorWithRed:234 / 255.0f green:234 / 255.0f blue:234 / 255.0f alpha:1];
    UILabel *labelOfTitle = [[UILabel alloc] init];
    [self.view addSubview:labelOfTitle];
    labelOfTitle.text = @"我的推广码:";
    labelOfTitle.textColor = [UIColor whiteColor];
    labelOfTitle.textAlignment = NSTextAlignmentRight;
    
    labelOfTitle.font = [UIFont systemFontOfSize:15];
    [labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(130);
        make.right.equalTo(self.view.mas_centerX);
        make.width.offset(100);
        make.height.offset(20);
        
    }];
    
    self.topLabel = [[UILabel alloc] init];
    [self.view addSubview:self.topLabel];
    self.topLabel.textColor = [UIColor whiteColor];
    self.topLabel.textAlignment = NSTextAlignmentLeft;
    self.topLabel.font = [UIFont systemFontOfSize:15];
    
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(130);
        make.left.equalTo(labelOfTitle.mas_right).with.offset(10);
        make.width.offset(100);
        make.height.offset(20);
        
    }];
    

}
#pragma mark - - - 中间带有图标二维码生成
- (void)setupGenerate_Icon_QRCode {
    
    // 1、借助UIImageView显示二维码
    self.imageView = [[UIImageView alloc] init];
    [self.view addSubview:self.imageView];

    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabel.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(120);
        make.right.equalTo(self.view).with.offset(-120);
        make.height.equalTo(self.imageView.mas_width);
        
        
    }];
    
    //http://112.74.36.208
    CGFloat scale = 0.2;
    NSString *str = @"http://app.lemaiquan.net/home/Register/regweb/";
    NSString *str2 = @"invitation/";
    NSString *str3 = [str stringByAppendingString:str2];
    NSString *strData = [str3 stringByAppendingString:invitation];

    // 2、将最终合得的图片显示在UIImageView上
    self.imageView.image = [SGQRCodeTool SG_generateWithLogoQRCodeData:strData logoImageName:@"liyi" logoScaleToSuperView:scale];
    
    UILabel *labelPhone = [[UILabel alloc] init];
    [self.view addSubview:labelPhone];
    labelPhone.text = @"长按保存二维码";
    labelPhone.textColor =[UIColor whiteColor];
    labelPhone.font = [UIFont systemFontOfSize:15];
    [labelPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).with.offset(4);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.offset(20);
        
    }];
    
    
    //1.创建长按手势
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
    
    //2.开启人机交互
    self.imageView.userInteractionEnabled = YES;
    
    //3.添加手势
    [self.imageView addGestureRecognizer:longTap];
    

    
}

#pragma mark 长按手势弹出警告视图确认
-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture

{
    
    if(gesture.state==UIGestureRecognizerStateBegan)
        
    {
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"保存图片" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"取消保存图片");
        }];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"确认保存图片");
            
            // 保存图片到相册
            UIImageWriteToSavedPhotosAlbum(self.imageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
            
            //            UIImageWriteToSavedPhotosAlbum(<#UIImage * _Nonnull image#>, <#id  _Nullable completionTarget#>, <#SEL  _Nullable completionSelector#>, <#void * _Nullable contextInfo#>)
            
        }];
        
        [alertControl addAction:cancel];
        [alertControl addAction:confirm];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }
    
}

#pragma mark 保存图片后的回调
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError:  (NSError*)error contextInfo:(id)contextInfo

{
    
    NSString*message =@"呵呵";
    
    if(!error) {
        
        message =@"成功保存到相册";
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }else
        
    {
        
        message = [error description];
        
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }
    
}


- (void)creationDate{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:tokenStr forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:My_QrCode
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       self.topLabel.text = dict1[@"invitation"];
                                       [PublicFunction saveConfig:dict1[@"invitation"] forKey:@"invitation"];
                                       invitation =dict1[@"invitation"];
                                       NSLog(@"aaaa%@", invitation);
                                       [self setupGenerate_Icon_QRCode];

                                                                    }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    


}

@end
